package cn.zhyjohn.paysystem.action;

import cn.zhyjohn.paysystem.biz.WeChatPayManager;
import cn.zhyjohn.paysystem.common.Response;
import cn.zhyjohn.paysystem.requset.WxRefundParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.IOException;

/**
 * @author JOHN
 * @title: WeChatController
 * @projectName paysystem
 * @since 2019/8/27
 */
@RestController
@RequestMapping("pay")
@Api(tags = "微信支付接口")
public class WeChatController {

    @Autowired
    private WeChatPayManager payManager;

    /**
     * 微信通知回调
     * @author JOHN
     * @date 16:43
     **/
    @RequestMapping(value = "/wxpay/pay_notify")
    @ApiIgnore
    public String payNotify(HttpServletRequest request) {
        return payManager.payNotify(request);
    }

    /**
     * 微信H5 支付 <br>
     * 必须再web页面中发起支付且域名已添加到开发配置中
     * @author JOHN
     * @date 14:55
     * @param total_fee 金额
     **/
    @PostMapping(value = "/wxpay/wapPay")
    @ApiOperation(value = "微信-H5支付")
    @ApiImplicitParam(name = "total_fee", value = "金额", required = true, paramType = "query", dataType = "double")
    public Response wapPay(HttpServletRequest request, @NotBlank String total_fee) {
        return payManager.wapPay(request, total_fee);
    }

    /**
     *  公众号支付
     * @author JOHN
     * @date 14:57
     * @param openId 微信用户id
     * @param total_fee 金额
     **/
    @PostMapping(value = "/wxpay/webPay")
    @ApiOperation(value = "微信-公众号支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "total_fee", value = "金额", required = true, paramType = "query", dataType = "double"),
            @ApiImplicitParam(name = "openId", value = "微信用户id", required = true, paramType = "query", dataType = "String")
    })
    public Response webPay(HttpServletRequest request, @NotBlank String openId, @NotBlank String total_fee) {
        return payManager.webPay(request, openId, total_fee);
    }

    /**
     * 扫码支付模式二
     * @author JOHN
     * @date 14:58
     * @param orderNo 订单号
     * @param total_fee 金额
     **/
    @GetMapping(value = "/wxpay/qrcodePay")
    @ApiOperation(value = "微信-生成支付二维码(模式二)", produces="image/png")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "total_fee", value = "金额", required = true, paramType = "query", dataType = "double")
    })
    public Response scanCodeOne(HttpServletRequest request, HttpServletResponse response, @NotBlank String orderNo, @NotBlank String total_fee) throws IOException, IOException {
        return payManager.scanCodeTwo(request, response, orderNo, total_fee);
    }

    /**
     * 微信APP支付
     *
     * @param body      描述
     * @param orderNo   订单号
     * @param total_fee 金额
     * @author JOHN
     * @date 15:31
     **/
    @PostMapping(value = "/wxpay/appPay")
    @ApiOperation(value = "微信-APP支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "body", value = "描述", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "total_fee", value = "金额", required = true, paramType = "query", dataType = "double")
    })
    public Response appPay(HttpServletRequest request, String body, String orderNo, String total_fee) {
        return payManager.appPay(request, body, orderNo, total_fee);
    }

    /**
     * 微信小程序支付
     *
     * @param openid    微信用户id
     * @param body      描述
     * @param orderNo   订单号
     * @param total_fee 金额
     * @author JOHN
     * @date 15:34
     **/
    @PostMapping(value = "/wxpay/smroutinePay")
    @ApiOperation(value = "微信-小程序支付")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openid", value = "微信用户id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "body", value = "描述", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "orderNo", value = "订单号", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "total_fee", value = "金额", required = true, paramType = "query", dataType = "double")
    })
    public Response jsapi(HttpServletRequest request, String openid, String body, String orderNo, String total_fee) {
        return payManager.jsapi(request, openid, body, orderNo, total_fee);
    }

    /**
     * 提现
     *
     * @param openId 微信用户id
     * @param body 描述
     * @param amount 金额
     * @author JOHN
     * @date 10:50
     **/
    @PostMapping(value = "/wxpay/transfers")
    @ApiOperation(value = "微信-提现")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "微信用户id", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "body", value = "描述", required = true, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "amount", value = "金额", required = true, paramType = "query", dataType = "double")
    })
    public Response transfers(HttpServletRequest request, String openId, String body, String amount) {
        return payManager.transfers(request, openId, body, amount);
    }

    /**
     * 微信退款
     * @author JOHN
     * @date 15:00
     **/
    @PostMapping(value = "/wxpay/refund")
    @ApiOperation(value = "微信-退款")
    public Response refund(@Valid WxRefundParams params) {
        return payManager.refund(params);
    }

}
